package com.example.demo20250926.service.impl;

import com.example.demo20250926.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.validation.constraints.Min;

/**
 * @author sky
 * @date 2025年10月31日 11:59
 * @description
 * @since 1.0.0
 */
@Service
public class MailServiceImpl implements MailService{
    private final static Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
    @Autowired
    private MailProperties mailProperties;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 发送简单文本的邮件
     *
     * @param to
     * @param subject
     * @param content
     * @return
     */
    @Override
    public boolean send(String to, String subject, String content) {
        logger.info("## Ready to send mail ...");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailProperties.getUsername());
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
            javaMailSender.send(message);
            logger.info("## Mail sent successfully.");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("## Mail sent failed.");
            return false;
        }
        return true;
    }

    /**
     * 发送 html 的邮件
     *
     * @param to
     * @param subject
     * @param html
     * @return
     */
    @Override
    public boolean sendWithHtml(String to, String subject, String html) {
        logger.info("## Ready to send mail ...");
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper=null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(html,true);
            javaMailSender.send(message);
            logger.info("## Mail sent successfully.");
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("## Mail sent failed.");
            return false;
        }
        return true;
    }

    /**
     * 发送带有图片的 html 的邮件
     *
     * @param to
     * @param subject
     * @param html
     * @param cids
     * @param filePaths
     * @return
     */
    @Override
    public boolean sendWithImageHtml(String to, String subject, String html, String[] cids, String[] filePaths) {
        logger.info("## Ready to send mail ...");
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper=null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(html,true);
            for (int i = 0; i < cids.length; i++) {
                FileSystemResource fileSystemResource = new FileSystemResource(filePaths[i]);
                // addInline() 方法 cid 需要 html 中的 cid (Content ID) 对应，才能设置图片成功，
                // 具体可以参见，下面 4.3.3 单元测试的参数设置
                helper.addInline(cids[i],fileSystemResource);
            }
            javaMailSender.send(message);
            logger.info("## Send the mail with image success.");
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("## Send html mail error:{}",e);
            return false;
        }
        return true;
    }

    /**
     * 发送带有附件的邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param filePaths
     * @return
     */
    @Override
    public boolean sendWithWithEnclosure(String to, String subject, String content, String[] filePaths) {

        logger.info("## Ready to send mail ...");
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper=null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            for (String filePath : filePaths) {
                FileSystemResource fileSystemResource = new FileSystemResource(filePath);
                String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
                helper.addAttachment(fileName,fileSystemResource);
            }
            javaMailSender.send(message);
            logger.info("## Send the mail with enclosure  success.");
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("## Send html mail error:{}",e);
            return false;
        }
        return true;
    }
}
